package com.example.contactfragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.boyuan.huijiaoyu.BaseActivity;
import com.boyuan.huijiaoyu.HomeContactDeail1Activity;
import com.boyuan.huijiaoyu.HomeContactDeail2Activity;
import com.boyuan.huijiaoyu.R;
import com.example.newfragment.DetailPerson;
import com.example.newfragment.RecentPerson;
import com.magicsoft.app.helper.ToastHelper;
import com.magicsoft.app.wcf.listener.GetOneRecordListener;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * 通讯录组织架构及会员
 * 
 * @author liqingjun
 * 
 */
public class HomeContactOrgActivity extends BaseActivity implements
		OnClickListener {

	// 返回
	private LinearLayout lin_back;
	// 搜索
	private LinearLayout lin_search;
	// 组织标题
	private LinearLayout lin_contact_header;
	// 组织架构标题列表
	private List<RecentPerson> orgTitleDatas = new ArrayList<RecentPerson>();
	// 组织架构
	private ArrayList<RecentPerson> orgDatas = new ArrayList<RecentPerson>();
	private HomeContactOrgAdapter orgAdapter;
	private ListView listview_org; // 组织架构
	private ContactService contactService;
	private DetailPerson homeContactDetailEntity;
	private String uid;
	private String yonghuID;
	private TextView backtitle;
	private String back;
	private boolean clickable = true;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home_contact_org);
		SharedPreferences sp = getSharedPreferences("a.xml", 0);
		yonghuID = sp.getString("id", "");
		prepareView();
		prepareData();
	}

	private void prepareView() {
		// 返回
		lin_back = (LinearLayout) findViewById(R.id.lin_back);
		lin_back.setOnClickListener(this);
		backtitle = (TextView) findViewById(R.id.backtitle);

		// 搜索
		lin_search = (LinearLayout) findViewById(R.id.lin_search);
		lin_search.setOnClickListener(this);
		// 组织标题
		lin_contact_header = (LinearLayout) findViewById(R.id.lin_contact_header);

		// 组织架构
		listview_org = (ListView) findViewById(R.id.listview_org);
		orgAdapter = new HomeContactOrgAdapter(HomeContactOrgActivity.this, orgDatas, imageLoader);
		listview_org.setAdapter(orgAdapter);
		listview_org.setOnItemClickListener(new OnItemClickListener() {


			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				RecentPerson homeContactOrgEntity = (RecentPerson) orgAdapter
						.getItem(position);

				String type = homeContactOrgEntity.getType();

				if ("org".equals(type)) {
					if (clickable) {
						orgTitleDatas.add(homeContactOrgEntity);
						addOrgTitle();
					}
				} else if ("user".equals(type)) {
					uid = homeContactOrgEntity.getUid();
					getContactDetail(uid);
				}
			}
		});

	}
	
	private void getContactDetail(final String contactId) {

		if (contactService == null) {
			contactService = new ContactService(getApplicationContext());
		}

		showLoading(getString(R.string.loading_data));

		contactService.getContactdetail(yonghuID, contactId,
				new GetOneRecordListener<DetailPerson>() {

					@Override
					public void onFinish(DetailPerson data) {
						hideLoading();
						homeContactDetailEntity = data;
						if(homeContactDetailEntity.getIs_common().equals("1")||homeContactDetailEntity.getIs_favorite().equals("1")){
							Intent intent = new Intent(HomeContactOrgActivity.this,HomeContactDeail2Activity.class);
							intent.putExtra("uid", uid);
							intent.putExtra("homeContactDetailEntity", homeContactDetailEntity);
							intent.putExtra("titleback", "通讯录");
							startActivity(intent);
//							finish();
						}else{
							Intent intent = new Intent(HomeContactOrgActivity.this,HomeContactDeail1Activity.class);
//							intent.putExtra("realname", homeContactDetailEntity.getRealname());
//							intent.putExtra("sex", homeContactDetailEntity.getSex());
//							intent.putExtra("account_mobile", homeContactDetailEntity.getAccount_mobile());
//							intent.putExtra("mobile", homeContactDetailEntity.getMobile());
							intent.putExtra("homeContactDetailEntity", homeContactDetailEntity);
							intent.putExtra("yonghuID", yonghuID);
							intent.putExtra("uid", uid);
							intent.putExtra("titleback", "通讯录");
							startActivity(intent);
//							finish();
						}

					}

					@Override
					public void onFailed(String Message) {
						hideLoading();
						ToastHelper.showMsg(HomeContactOrgActivity.this,
								Message, false);
					}
				});
	}

	private void prepareData() {

		Intent intent = getIntent();
		RecentPerson homeContactOrgEntity = (RecentPerson) intent
				.getSerializableExtra("homeContactOrgEntity");
		orgTitleDatas.add(homeContactOrgEntity);
		addOrgTitle();
	}

	private void addOrgTitle() {
		clickable = false;
		int lenth = orgTitleDatas.size();
		lin_contact_header.removeAllViews();

		for (int i = 0; i < lenth; i++) {

			final int position = i;
			RecentPerson homeContactOrgEntity = orgTitleDatas.get(i);
			if (i > 0) {
				ImageView imageView = new ImageView(HomeContactOrgActivity.this);
				imageView.setBackgroundResource(R.drawable.arrow);
				lin_contact_header.addView(imageView);
			}
			TextView textView = new TextView(HomeContactOrgActivity.this);
			textView.setText(homeContactOrgEntity.getName());
			textView.setTextSize(14);

			if (i == lenth - 1) {
				getOrgDatas(homeContactOrgEntity.getUid());
				textView.setTextColor(getResources()
						.getColor(R.color.form_edit));
			} else {
				textView.setTextColor(getResources().getColor(
						R.color.home_tab_txt_sel_color));
				textView.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						orgTitleDatas = orgTitleDatas.subList(0, position + 1);
						addOrgTitle();
					}
				});
			}

			lin_contact_header.addView(textView);
		}

	}

	private void getOrgDatas(String orgID) {
		
		if (contactService == null) {
			contactService = new ContactService(HomeContactOrgActivity.this);
		}
		contactService.getOrgChildDatas(orgID,
				new GetOneRecordListener<List<RecentPerson>>() {

					@Override
					public void onFinish(List<RecentPerson> data) {
						
						orgDatas.clear();
						if (data != null) {
							orgDatas.addAll(data);
						}
						clickable = true;
						orgAdapter.notifyDataSetChanged();
					}

					@Override
					public void onFailed(String Message) {
						clickable = true;
						orgDatas.clear();
						orgAdapter.notifyDataSetChanged();
					}
				});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.lin_back:
			onBackPressed();
			break;
		case R.id.lin_search:
			// 搜索
			Intent searchIntent = new Intent(HomeContactOrgActivity.this,
					HomeContactSearchActivity.class);
			searchIntent.putExtra("isOnlyShowHistory", true);
			startActivity(searchIntent);
			break;
		default:
			break;
		}
	}

	@Override
	public void backClicked() {
		if (isShowingLoading()) {
			hideLoading();
		} else {
			setResult(RESULT_OK);
			isBackAllowed = true;
		}
	}

}
